package com.xzc.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.xzc.enums.ExportColumnEnums;
import com.xzc.pojo.AllStaffTime;
import com.xzc.pojo.StaffAllProjectTime;
import com.xzc.pojo.StaffProjectTime;
import com.xzc.pojo.ZenTaoValues;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 熊志聪
 * @Classname ExportUtils
 * @Description 打印导出工具类
 * @Date 2021/8/5 10:39
 */
@Slf4j
public class ExportUtils {
    /**
     * sonarqube建议 创建私有构造器
     */
    private ExportUtils(){
        throw new IllegalStateException("ExportUtils class");
    }
    /**
     * 使用该方法的前提是web请求
     * excel打印导出
     * @param data  打印的数据源 size>0
     * @param exportAlias 打印的表头
     * @param zenTaoValues  文件信息
     */
    public  static  String export(List<AllStaffTime> data, List<String> exportAlias, ZenTaoValues zenTaoValues){
        ExcelWriter writer = ExcelUtil.getWriter(zenTaoValues.getFileName());
        for (int i = 0; i < exportAlias.size(); i++) {
            writer.setColumnWidth(i,40);
        }
        //sheet名称设置
        writer.renameSheet(zenTaoValues.getMonth()+"月");
        //表头
        writer.writeHeadRow(exportAlias);
        //正文
        int rowIndex=0;
        for(AllStaffTime allStaffTime:data){
            String oneDeptName=allStaffTime.getOneDeptName();
            String twoDeptName=allStaffTime.getTwoDeptName();
            List<StaffAllProjectTime> staffAllProjectTimes = allStaffTime.getStaffAllProjectTimes();
            if(CollUtil.isEmpty(staffAllProjectTimes)){
                continue;
            }
            for(StaffAllProjectTime staffAllProjectTime: staffAllProjectTimes){
                String staffName=staffAllProjectTime.getStaffName();
                List<StaffProjectTime> staffProjectTimes = staffAllProjectTime.getStaffProjectTimes();
                BigDecimal totalMonthlyWorkTime=BigDecimal.ZERO;
                int hasUsedRows=0;
                for(StaffProjectTime staffProjectTime: staffProjectTimes){
                    BigDecimal workTime = staffProjectTime.getWorkTime();
                    if(staffProjectTimes.size()>1&&workTime.compareTo(BigDecimal.ZERO)==0){
                        continue;
                    }
                    List<Object> row=new ArrayList<>();
                    row.add(oneDeptName);row.add(twoDeptName);row.add(staffName);
                    row.add(staffProjectTime.getProjectName());row.add(staffProjectTime.getProjectCode());
                    if(workTime.compareTo(BigDecimal.ZERO)>0) {
                        row.add(workTime);row.add(workTime);
                    }

                    totalMonthlyWorkTime = totalMonthlyWorkTime.add(workTime);
                    writer.writeRow(row);
                    ++hasUsedRows;
                    ++rowIndex;
                }
                //存在大于1条的项目记录：姓名那一列需要合并
                int firstRow=rowIndex-hasUsedRows;
                if(staffProjectTimes.size()>1&&(firstRow!=rowIndex)){
                    //staffName totalMonthlyWorkTime
                    writer.merge(firstRow+1,rowIndex,2,2,null,true);
                    //
                    writer.merge(firstRow+1,rowIndex,6,6,String.valueOf(totalMonthlyWorkTime),false);

                }
            }
        }

        try {
            writer.flush();

        }catch (IORuntimeException e){
            log.error("打印异常:{}",e.getLocalizedMessage());
        }finally {
            writer.close();
        }

        return "打印成功";
    }

    public static void main(String[] args) {
        List<AllStaffTime> data =new ArrayList<>();
        AllStaffTime allStaffTime1 = new AllStaffTime();
        allStaffTime1.setOneDeptName("1");
        allStaffTime1.setTwoDeptName("1-1");
        List<StaffAllProjectTime> staffAllProjectTimes=new ArrayList<>();
        StaffAllProjectTime staffAllProjectTime=new StaffAllProjectTime();
        staffAllProjectTime.setStaffName("员工1");
        List<StaffProjectTime> staffProjectTimes1=new ArrayList<>();
        staffProjectTimes1.add(new StaffProjectTime("项目名称1","项目编码1", BigDecimal.ONE));
        staffProjectTimes1.add(new StaffProjectTime("项目名称2","项目编码2", BigDecimal.ONE));
        staffProjectTimes1.add(new StaffProjectTime("项目名称3","项目编码3", BigDecimal.ONE));
        staffAllProjectTime.setStaffProjectTimes(staffProjectTimes1);
        staffAllProjectTimes.add(staffAllProjectTime);
        allStaffTime1.setStaffAllProjectTimes(staffAllProjectTimes);
        data.add(allStaffTime1);

        //---------------------------------
        AllStaffTime allStaffTime2 = new AllStaffTime();
        allStaffTime2.setOneDeptName("1");
        allStaffTime2.setTwoDeptName("1-2");
        List<StaffAllProjectTime> staffAllProjectTimes2=new ArrayList<>();
        StaffAllProjectTime staffAllProjectTime2=new StaffAllProjectTime();
        staffAllProjectTime2.setStaffName("员工2");
        List<StaffProjectTime> staffProjectTimes2=new ArrayList<>();
        staffProjectTimes2.add(new StaffProjectTime("项目名称1","项目编码1", BigDecimal.ONE));
        staffProjectTimes2.add(new StaffProjectTime("项目名称2","项目编码2", BigDecimal.ONE));
        staffProjectTimes2.add(new StaffProjectTime("项目名称3","项目编码3", BigDecimal.ONE));
        staffAllProjectTime2.setStaffProjectTimes(staffProjectTimes2);
        staffAllProjectTimes2.add(staffAllProjectTime2);
        allStaffTime2.setStaffAllProjectTimes(staffAllProjectTimes2);
        data.add(allStaffTime2);
        //---------------------------------



        ZenTaoValues zenTaoValues=new ZenTaoValues();
        zenTaoValues.setMonth(11);
        zenTaoValues.setFileName("C:\\Users\\熊志聪\\Desktop\\1\\show2.xls");
        export(data, ExportColumnEnums.getAlias(0),zenTaoValues);
    }

}
